<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
    <title id="bv20941">CREATE LANGUAGE</title>
    <body>
        <p id="sql_command_desc">Defines a new procedural language.</p>
        <section id="section2">
            <title>Synopsis</title>
            <codeblock id="sql_command_synopsis">CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <varname>name</varname>

CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <varname>name</varname>
    HANDLER <varname>call_handler</varname> [ INLINE <varname>inline_handler</varname> ] 
   [ VALIDATOR <varname>valfunction</varname> ]
            </codeblock>
        </section>
        <section id="section3">
            <title>Description</title>
            <p><codeph>CREATE LANGUAGE</codeph> registers a new procedural language with a Greenplum
                database. Subsequently, functions and trigger procedures can be defined in this new
                language.</p> 
            <note>Procedural languages for Greenplum Database have been made into "extensions," and
                should therefore be installed with <xref href="CREATE_EXTENSION.xml#topic1"/>, not
                    <codeph>CREATE LANGUAGE</codeph>. Using <codeph>CREATE LANGUAGE</codeph>
                directly should be restricted to extension installation scripts. If you have a
                "bare" language in your database, perhaps as a result of an upgrade, you can convert
                it to an extension using <codeph>CREATE EXTENSION <varname>langname</varname> FROM
                    unpackaged</codeph>. </note>
            <p>Superusers can register a new language with a Greenplum database. A database owner
                can also register within that database any language listed in the
                    <codeph>pg_pltemplate</codeph> catalog in which the
                    <codeph>tmpldbacreate</codeph> field is true. The default configuration allows
                only trusted languages to be registered by database owners. The creator of a
                language becomes its owner and can later drop it, rename it, or assign ownership to
                a new owner.</p>
            <p><codeph>CREATE OR REPLACE LANGUAGE</codeph> will either create a
                new language, or replace an existing definition. If the language
                already exists, its parameters are updated according to the values
                specified or taken from <varname>pg_pltemplate</varname>,
                but the language's ownership and permissions settings do not change,
                and any existing functions written in the language are assumed to still
                be valid. In addition to the normal privilege requirements for creating
                a language, the user must be superuser or owner of the existing language.
                The <codeph>REPLACE</codeph> case is mainly meant to be used to
                ensure that the language exists. If the language has a
                <varname>pg_pltemplate</varname> entry then <codeph>REPLACE</codeph>
                will not actually change anything about an existing definition, except in
                the unusual case where the <varname>pg_pltemplate</varname> entry
                has been modified since the language was created.
            </p>
            <p><codeph>CREATE LANGUAGE</codeph> effectively associates the language name with
                handler function(s) that are responsible for running functions written in that language. For a
                function written in a procedural language (a language other than C or SQL), the
                database server has no built-in knowledge about how to interpret the function's
                source code. The task is passed to a special handler that knows the details of the
                language. The handler could either do all the work of parsing, syntax analysis,
                execution, and so on or it could serve as a bridge between Greenplum Database and an
                existing implementation of a programming language. The handler itself is a C
                language function compiled into a shared object and loaded on demand, just like any
                other C function. Therese procedural language packages are included in the standard
                Greenplum Database distribution: PL/pgSQL, PL/Perl, and PL/Python. Language handlers
                have also been added for PL/Java and PL/R, but those languages are not pre-installed
                with Greenplum Database. See the topic on <xref
                    href="https://www.postgresql.org/docs/9.4/xplang.html" scope="external"
                    format="html">Procedural Languages</xref> in the PostgreSQL documentation for
                more information on developing functions using these procedural languages.</p>
            <p>The PL/Perl, PL/Java, and PL/R libraries require the correct versions of Perl, Java,
                and R to be installed, respectively. </p>
            <p otherprops="pivotal">On RHEL and SUSE platforms, download the appropriate extensions
                from <xref href="https://network.pivotal.io/products/pivotal-gpdb" scope="external" format="html">VMware Tanzu
                    Network</xref>, then install the extensions using the Greenplum Package Manager
                    (<codeph>gppkg</codeph>) utility to ensure that all dependencies are installed
                as well as the extensions. See the Greenplum Database Utility Guide for details
                about <codeph>gppkg</codeph>. </p>
            <p>There are two forms of the <codeph>CREATE LANGUAGE</codeph> command. In the first
                form, the user specifies the name of the desired language and the Greenplum Database
                server uses the <codeph>pg_pltemplate</codeph> system catalog to determine the
                correct parameters. In the second form, the user specifies the language parameters
                as well as the language name. You can use the second form to create a language that
                is not defined in <codeph>pg_pltemplate</codeph>. </p>
            <p>When the server finds an entry in the <codeph>pg_pltemplate</codeph> catalog for the
                given language name, it will use the catalog data even if the command includes
                language parameters. This behavior simplifies loading of old dump files, which are
                likely to contain out-of-date information about language support functions.</p>
        </section>
        <section id="section4">
            <title>Parameters</title>
            <parml>
                <plentry>
                    <pt>TRUSTED</pt>
                    <pd><codeph>TRUSTED</codeph> specifies that the language does
                        not grant access to data that the user would not otherwise
                        have. If this key word is omitted
                        when registering the language, only users with the
                        Greenplum Database superuser privilege can
                        use this language to create new functions.</pd>
                </plentry>
                <plentry>
                    <pt>PROCEDURAL</pt>
                    <pd>This is a noise word.</pd>
                </plentry>
                <plentry>
                    <pt>
                        <varname>name</varname>
                    </pt>
                    <pd>The name of the new procedural language. The name must be unique among the
                        languages in the database. Built-in support is included for
                            <codeph>plpgsql</codeph>, <codeph>plperl</codeph>, and
                            <codeph>plpythonu</codeph>. The languages <codeph>plpgsql</codeph>
                        (PL/pgSQL) and <codeph>plpythonu</codeph> (PL/Python) are installed by
                        default in Greenplum Database.</pd>
                </plentry>
                <plentry>
                    <pt>HANDLER <varname>call_handler</varname></pt>
                    <pd>Ignored if the server has an entry for the specified language name in
                            <codeph>pg_pltemplate</codeph>. The name of a previously registered
                        function that will be called to run the procedural language functions.
                        The call handler for a procedural language must be written in a compiled
                        language such as C with version 1 call convention and registered with
                        Greenplum Database as a function taking no arguments and returning the
                            <codeph>language_handler</codeph> type, a placeholder type that is
                        simply used to identify the function as a call handler.</pd>
                </plentry>
                <plentry>
                    <pt>INLINE <varname>inline_handler</varname></pt>
                    <pd>The name of a previously registered function that is called to run an
                        anonymous code block in this language that is created with the <codeph><xref
                                href="DO.xml#topic1"/></codeph> command. If an
                            <codeph>inline_handler</codeph> function is not specified, the language
                        does not support anonymous code blocks. The handler function must take one
                        argument of type <codeph>internal</codeph>, which is the <codeph><xref
                                href="DO.xml#topic1"/></codeph> command internal representation. The
                        function typically return <codeph>void</codeph>. The return value of the
                        handler is ignored. </pd>
                </plentry>
                <plentry>
                    <pt>VALIDATOR <varname>valfunction</varname></pt>
                    <pd>Ignored if the server has an entry for the specified language name in
                            <codeph>pg_pltemplate</codeph>. The name of a previously registered
                        function that will be called to run the procedural language functions.
                        The call handler for a procedural language must be written in a compiled
                        language such as C with version 1 call convention and registered with
                        Greenplum Database as a function taking no arguments and returning the
                            <codeph>language_handler</codeph> type, a placeholder type that is
                        simply used to identify the function as a call handler.</pd>
                </plentry>
            </parml>
        </section>
        <section id="section5">
            <title>Notes</title>
            <p>The PL/pgSQL language is already registered in all databases by default. The
                PL/Python language extension is installed but not registered.</p>
            <p>The system catalog <codeph>pg_language</codeph> records information about the
                currently installed languages.</p>
            <p>To create functions in a procedural language, a user must have the
                    <codeph>USAGE</codeph> privilege for the language. By default,
                    <codeph>USAGE</codeph> is granted to <codeph>PUBLIC</codeph> (everyone) for
                trusted languages. This may be revoked if desired. </p>
            <p>Procedural languages are local to individual databases. You create and drop languages
                for individual databases.</p>
            <p>The call handler function and the validator function (if any) must already exist if
                the server does not have an entry for the language in
                <codeph>pg_pltemplate</codeph>. But when there is an entry, the functions need not
                already exist; they will be automatically defined if not present in the database. </p>
            <p>Any shared library that implements a language must be located in the same
                    <codeph>LD_LIBRARY_PATH</codeph> location on all segment hosts in your Greenplum
                Database array.</p>
        </section>
        <section id="section6">
            <title>Examples</title>
            <p>The preferred way of creating any of the standard procedural languages is to use
                    <codeph>CREATE EXTENSION</codeph> instead of <codeph>CREATE LANGUAGE</codeph>.
                For example:</p>
            <codeblock>CREATE EXTENSION plperl;</codeblock>
            <p>For a language not known in the <codeph>pg_pltemplate</codeph> catalog:</p>
            <codeblock>CREATE FUNCTION plsample_call_handler() RETURNS 
language_handler
    AS '$libdir/plsample'
    LANGUAGE C;
CREATE LANGUAGE plsample
    HANDLER plsample_call_handler;</codeblock>
        </section>
        <section id="section7">
            <title>Compatibility</title>
            <p><codeph>CREATE LANGUAGE</codeph> is a Greenplum Database extension. </p>
        </section>
        <section id="section8">
            <title>See Also</title>
            <p><codeph><xref href="ALTER_LANGUAGE.xml#topic1" type="topic" format="dita"/></codeph>,
                        <codeph><xref href="CREATE_EXTENSION.xml" type="topic" format="dita"
                    /></codeph>, <codeph><xref href="CREATE_FUNCTION.xml#topic1" type="topic"
                        format="dita"/></codeph>, <codeph><xref href="./DROP_EXTENSION.xml#topic1"
                        type="topic" format="dita"/></codeph>, <codeph><xref
                        href="./DROP_LANGUAGE.xml#topic1" type="topic" format="dita"/></codeph>,
                        <codeph><xref href="./GRANT.xml#topic1" type="topic" format="dita"
                    /></codeph>
                <codeph><xref href="DO.xml#topic1"/></codeph></p>
        </section>
    </body>
</topic>
